/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.training1.core.common.dao.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;





import com.training1.core.common.entity.DataEntity;
import com.training1.core.common.global.signIn.BOXLoginInfo;
import com.training1.core.common.global.signIn.BOXLoginInfoUtil;
//import com.training1.core.common.global.signIn.LoginInfo;
//import com.training1.core.common.global.signIn.LoginInfoUtil;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * 用于更新用户的创建人，创建时间等信息
 * @author hl
 * @version 2015-12-23
 */
@Intercepts({@Signature(type = Executor.class, method = "update",
        args = {MappedStatement.class, Object.class})})
public class UpdateBaseInformationInterceptor implements Interceptor {

	
	
    @SuppressWarnings("unchecked")
	@Override
    public Object intercept(Invocation invocation) throws Throwable {

        Object parameter = invocation.getArgs()[1];
//        LoginInfo info = LoginInfoUtil.getLoginInfo();
        BOXLoginInfo info = BOXLoginInfoUtil.getLoginInfo();
        Date now = new Date();
        if(parameter instanceof DataEntity){
        	DataEntity entity = (DataEntity)parameter;
        	entity.setCreateTime(now);
        	entity.setUpdateTime(now);
//        	entity.setCreateBy(info.getLoginName());
//        	entity.setUpdateBy(info.getLoginName());
        	entity.setCreator(info.getLoginName());
        	entity.setUpdater(info.getLoginName());
        }
        if(parameter instanceof Map){
        	Map<String, Object> map = (Map<String, Object>)parameter;
        	map.put("createTime", now);
        	map.put("updateTime", now);
//        	map.put("createBy", info.getLoginName());
//        	map.put("updateBy", info.getLoginName());
        	map.put("creator", info.getLoginName());
        	map.put("updater", info.getLoginName());
        }
        return invocation.proceed();
    }


    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }
}
